﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using NHibernate;
using NHibernate.Linq;

namespace BootCms.Environment
{
    public class NHibernateTenantRepository : ITenantRepository 
    { 
        private readonly ISession session; 
        private readonly HttpContextBase context;

        public NHibernateTenantRepository(ISession session, HttpContextBase context)
        { 
            this.session = session; 
            this.context = context; 
        } 
        
        public Tenant Current { 
            get { 
                var host = context.Request.Headers["Host"];
                return FindByHost(host); 
            } 
        } 
        
        public Tenant FindByHost(string host) {
            return session.Query<Tenant>().SingleOrDefault(t => t.Domain == host);
        } 
    }

    public interface ITenantRepository
    {
        Tenant Current { get; }
        Tenant FindByHost(string host);
    }
}